//
//  AddManagerCon.m
//  ShopDemo
//
//  Created by Sunshine on 2018/7/18.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "AddManagerCon.h"
#import "AddManagerEditCon.h"
@interface AddManagerCon ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *listTableview;
@property(nonatomic,strong)NSArray *addressArray;
@end

@implementation AddManagerCon

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString *strurl = [NSString stringWithFormat:@"/dashop/address/app/list.do?USER_ID=%@",[UserDefault objectForKey:LogUserID]];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
    [self showDataMethodWithUrl:endstr];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHeaderViewMethod];
    [self creatTableviewMethod];
}

-(void)showDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        self.addressArray = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.listTableview reloadData];
        }];
    } failBlock:^(NSError *error) {
        
    }];
}

-(void)creatHeaderViewMethod{
    UIView * addBview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50*kJLXWidthScale)];
    addBview.userInteractionEnabled = YES;
    [self.view addSubview:addBview];
    
    UIImageView *imagevi = [UIImageView new];
    imagevi.image = [UIImage imageNamed:@"add_icon.png"];
    [addBview addSubview:imagevi];
    imagevi.sd_layout
    .centerYEqualToView(addBview)
    .leftSpaceToView(addBview, 10*kJLXWidthScale)
    .widthIs(40*kJLXWidthScale)
    .heightIs(40*kJLXWidthScale);
    
    UILabel *lab = [UILabel new];
    lab.text = @"新增地址";
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
    [addBview addSubview:lab];
    lab.sd_layout
    .centerYEqualToView(addBview)
    .leftSpaceToView(imagevi, 8*kJLXWidthScale)
    .rightSpaceToView(addBview, 50*kJLXWidthScale)
    .heightIs(30*kJLXWidthScale);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(AddressManagerAction:)];
    [addBview addGestureRecognizer:tap];
    
    
}

-(void)AddressManagerAction:(UITapGestureRecognizer *)tap{
    AddManagerEditCon *editVC = [[AddManagerEditCon alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:editVC animated:YES];
}

-(void)creatTableviewMethod{
    self.listTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 60*kJLXWidthScale, kScreenWidth, kScreenHeight - 120*kJLXWidthScale) style:UITableViewStylePlain];
    [self.view addSubview:self.listTableview];
    if (kScreenHeight == 812) {
        self.listTableview.sd_layout
        .bottomSpaceToView(self.view, 34);
    }
    self.listTableview.dataSource = self;
    self.listTableview.delegate = self;
    [self.listTableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"onecell"];
    //分隔线
    self.listTableview.separatorStyle = UITableViewCellAccessoryNone;
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
    
}

#pragma mark --UITableViewDelegate,UITableViewDataSource--


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.addressArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    NSDictionary *adddic = self.addressArray[indexPath.row];
    NSString *address = [NSString stringWithFormat:@"%@ %@\n%@：%@",adddic[@"ADDR_CITY"],adddic[@"ADDR_DETAILS"],adddic[@"ADDR_REALNAME"],adddic[@"ADDR_PHONE"]];
    UILabel *contentlab = [UILabel new];
    contentlab.text = address;
    contentlab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
    contentlab.numberOfLines = 0;
    [cell.contentView addSubview:contentlab];
    contentlab.sd_layout
    .topSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .leftSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .rightSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .autoHeightRatio(0);
    
    if ([adddic[@"IS_DEFAULT"] integerValue]==1) {
        UILabel *defaulab = [UILabel new];
        defaulab.text = @"默认";
        defaulab.layer.masksToBounds = YES;
        defaulab.layer.cornerRadius = 3*kJLXWidthScale;
        defaulab.textColor = [UIColor whiteColor];
        defaulab.textAlignment = NSTextAlignmentCenter;
        defaulab.font = [UIFont systemFontOfSize:13*kJLXWidthScale];
        defaulab.backgroundColor = [UIColor redColor];
        [cell.contentView addSubview:defaulab];
        defaulab.sd_layout
        .topSpaceToView(contentlab, 5*kJLXWidthScale)
        .leftSpaceToView(cell.contentView, 8*kJLXWidthScale)
        .heightIs(20*kJLXWidthScale)
        .widthIs(30*kJLXWidthScale);
        
    }
    
    NSArray *arr = @[@"dele.png",@"edit.png"];
    for (int i = 0; i < 2; i ++) {
        MineButton *btn = [MineButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 330 + i;
        btn.usinfo = adddic;
        [btn setImage:[UIImage imageNamed:arr[i]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(addressButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn];
        btn.sd_layout
        .topSpaceToView(contentlab, 5*kJLXWidthScale)
        .rightSpaceToView(cell.contentView, 8*kJLXWidthScale + (30*kJLXWidthScale + 15*kJLXWidthScale)*i)
        .heightIs(35*kJLXWidthScale)
        .widthIs(30*kJLXWidthScale);
    }
    return cell;
    
}


-(void)addressButtonAction:(MineButton *)sender{
    if (sender.tag == 330) {
        NSString *strurl = [NSString stringWithFormat:@"/dashop/address/app/delete.do?ADDRESS_ID=%@",sender.usinfo[@"ADDRESS_ID"]];
        NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
        [self addressMethodWithUrl:endstr];
    }else{
        AddManagerEditCon *editvc = [[AddManagerEditCon alloc]init];
        editvc.addressDic = sender.usinfo;
        [self.navigationController pushViewController:editvc animated:YES];
    }
    
}

-(void)addressMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            if ([dic[@"RESULT"] integerValue] == 1) {
                NSString *strurl = [NSString stringWithFormat:@"/dashop/address/app/list.do?USER_ID=%@",[UserDefault objectForKey:LogUserID]];
                NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
                [self showDataMethodWithUrl:endstr];
            }
        }];
    } failBlock:^(NSError *error) {
        
    }];
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *adddic = self.addressArray[indexPath.row];
    NSString *address = [NSString stringWithFormat:@"%@ %@\n%@：%@",adddic[@"ADDR_CITY"],adddic[@"ADDR_DETAILS"],adddic[@"ADDR_REALNAME"],adddic[@"ADDR_PHONE"]];
    CGRect rect = [address boundingRectWithSize:CGSizeMake(kScreenWidth, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    return rect.size.height+35*kJLXWidthScale + 22*kJLXWidthScale;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *adddic = self.addressArray[indexPath.row];
    NSString *address = [NSString stringWithFormat:@"%@ %@\n%@：%@",adddic[@"ADDR_CITY"],adddic[@"ADDR_DETAILS"],adddic[@"ADDR_REALNAME"],adddic[@"ADDR_PHONE"]];
    self.defalAddBlock(address,adddic);
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
